package com.notification;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import android.os.Environment;
import android.util.Log;

public class NotificationSetting implements Serializable {

	private static final long serialVersionUID = -4856204237521403543L;
	final String TAG = "NotificationSetting";
	final File path = new File(Environment.getExternalStorageDirectory().getPath() + "/WakeMeUp/");
	private String voice = "";
	private int[] gesture;
	private boolean isVoice = false, isGesture = false;

	public NotificationSetting() {
		gesture = new int[1];
		gesture[0] = 0;
	}

	public NotificationSetting(String voice, int[] gesture, boolean isVoice, boolean isGesture) {
		this.gesture = gesture;
		this.voice = voice;
		this.isGesture = isGesture;
		this.isVoice = isVoice;
	}
	
	public void save() {
		NotificationSetting setting = new NotificationSetting(voice, gesture, isVoice, isGesture);
		ObjectOutputStream oos = null;
		try {
			path.mkdirs();
			oos = new ObjectOutputStream(new FileOutputStream(path+"/"+TAG));
			oos.writeObject(setting);
		} catch (FileNotFoundException e) {
			Log.e(TAG, "File creation error\n");
			e.printStackTrace();
		} catch (IOException e) {
			Log.e(TAG, "I/O Exception");
		} finally {
			try {
				if (oos != null){
					oos.flush();
					oos.close();
				}
			} catch (IOException e) {
				Log.e(TAG, "Close failure");
			}
		}
		Log.d(TAG, "gestures:" + setting.getGestureStr());
	}
	
	public void load() {
		Log.d(TAG, "NotificationSetting.load()");
		NotificationSetting setting = new NotificationSetting();
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream(path+"/"+TAG));
			setting = (NotificationSetting) ois.readObject();
			this.gesture = setting.getGesture();
			this.voice = setting.getVoice();
			this.isGesture = setting.isGesture();
			this.isVoice = setting.isVoice();
		} catch (FileNotFoundException e) {
			Log.d(TAG, "File not found error: " + e.getMessage());
		} catch (IOException e) {
			Log.d(TAG, "I/O Exception: " + e.getMessage());
		} catch (ClassNotFoundException e) {
			Log.e(TAG, "Class not found: " + e.getMessage());
		} finally {
			try {
				if (ois != null)
					ois.close();
			} catch (IOException e) {
				Log.e(TAG, "Close failure: " + e.getMessage());
			}
		}
	}

	public void setVoice(String voice){
		this.voice = voice;
	}

	public void setGesture(int[] gesture){
		for (int i=1;i<gesture.length;i++)
			if(gesture[i-1]==0)
				gesture[i]=0;
		this.gesture = gesture;
	}

	public String getVoice() {
		return voice;
	}

	public int[] getGesture() {
		return gesture;
	}

	public boolean isVoice() {
		return isVoice;
	}

	public boolean isGesture() {
		return isGesture;
	}
	
	public boolean existVoice() {
		return !voice.isEmpty();
	}

	public boolean existGesture() {
		return !(gesture==null || gesture.length==0 || gesture[0]==0);
	}

	public String getGestureStr() {
		String rtn = "";
		for (int c: gesture)
			rtn += c;
		return rtn;
	}

	public void setVoiceOn(boolean checked) {
		isVoice = checked;
		if (checked)
			isGesture = false;
	}
	public void setGestureOn(boolean checked) {
		isGesture = checked;
		if (checked)
			isVoice = false;
	}
}
